//logs.js
const util = require('../../utils/util.js')
const { $Toast } = require('../../dist/base/index');
var api = require('../../config/api.js');
const app = getApp();

Page({
  data: {

    loading:false,
    timeInterval: 0,
    roomIds:[],
    rooms:[],
    roomIndex:0,

    actions: [
      {
        name: '删除',
        color: '#fff',
        fontsize: '20',
        width: 100,
        icon: 'close',
        background: '#ed3f14'
      }
    ],

    list:[],

    /*表单参数*/
  },
  onLoad: function () {
    this.getList(this.data.timeInterval);
  },

  handleChange({ detail }) {
    this.setData({
      timeInterval: detail.key,
      loading: true
    });
    this.getList(detail.key);
  },

  getList(timeInterval){
    let self = this;
    util.request(`${api.GetTimeIntervalList}?timeInterval=${timeInterval}`, {
    }, 'POST').then(res => {
      this.setData({
        loading: false
      });
      if (res.success) {
        this.setData({
          list: res.data
        });
      }
    }).catch(err => {
      this.setData({
        loading: false
      });
    });
    util.request(`${api.GetNeedTimeIntervalList}?timeInterval=${timeInterval}`, {
    }, 'POST').then(res => {
      let roomIds = []
      for (let i=0; i<res.data.length; i++){
        roomIds.push({'index':i, 'id':res.data[i].id})
      }
      self.setData({
        rooms: res.data,
        roomIds: roomIds
      })
    }).catch(err => {
      console.log(err)
    });
  },


  removeRoom(e){
    let self = this;
    this.setData({
      loading: true
    });
    util.request(`${api.RemoveTimeInterval}?timeInterval=${this.data.timeInterval}&id=${e.currentTarget.dataset.id}`, {
    }, 'POST').then(res => {
      this.setData({
        loading: false
      });
      if (res.success) {
        self.getList(self.data.timeInterval);
      }
    }).catch(err => {
      this.setData({
        loading: false
      });
    });

  },

  setRoomClick(){

  },

  clickFloor(e){
    this.setData({
      loading: true
    });
    let roomId;
    for (let i = 0; i < this.data.roomIds.length; i++){
      if (this.data.roomIds[i].index == e.detail.value){
        roomId = this.data.roomIds[i].id;
        break;
      }
    }
    let self = this;
    util.request(`${api.BindTimeIntervalRoom}?timeInterval=${this.data.timeInterval}&roomId=${roomId}`, {
    }, 'POST').then(res => {
      this.setData({
        loading: false
      });
      if (res.success) {
        self.getList(self.data.timeInterval);
      }
    }).catch(err => {
      this.setData({
        loading: false
      });
    });
  }



})
